﻿using CarRental.DDL;
using CarRental.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Windows.Forms;

namespace CarRental.View {
    public partial class AddCar : System.Web.UI.Page {
        DbAccess _dbAccess = new DbAccess();

        protected void Page_Load(object sender, EventArgs e) {
            if (!IsPostBack) {
                tbBuyInDate.Text = DateTime.Now.ToString("yyyy-MM-dd");

                ddlDepot.DataSource = new CarRental.Helpers.OptionItems.DepotLst(false);
                ddlDepot.DataValueField = "Key";
                ddlDepot.DataTextField = "Title";
                ddlDepot.DataBind();

                ddlDrivetrains.DataSource = new CarRental.Helpers.OptionItems.DriveTrainsLst(false);
                ddlDrivetrains.DataValueField = "Key";
                ddlDrivetrains.DataTextField = "Title";
                ddlDrivetrains.DataBind();

                ddlColor.DataSource = new CarRental.Helpers.OptionItems.ColorLst(false);
                ddlColor.DataValueField = "Key";
                ddlColor.DataTextField = "Title";
                ddlColor.DataBind();

                ddlCarType.DataSource = new CarRental.Helpers.OptionItems.CarTypeLst(false);
                ddlCarType.DataValueField = "Key";
                ddlCarType.DataTextField = "Title";
                ddlCarType.DataBind();

                ddlShop.DataSource = new CarRental.Helpers.OptionItems.ShopLst(false);
                ddlShop.DataValueField = "Key";
                ddlShop.DataTextField = "Title";
                ddlShop.DataBind();

                tbCarNo.Text = _dbAccess.GetSeqNo("car");

            }
        }

        protected void btnAddCar_Click(object sender, EventArgs e) {
            string _carNo = tbCarNo.Text;
            var _lst = _dbAccess.GetCar().Where(w => w.car_no.ToUpper() == _carNo.Trim().ToUpper()).ToList<car>();

            if (_lst.Any()) {
                lblWarning.Text = "Car number already exist. Please confirm the Car number.";
            } else {
                lblWarning.Text = "";
                car _car = new car();
                _car.car_no = tbCarNo.Text;
                _car.car_typecode = ddlCarType.SelectedValue;
                _car.car_model = tbCarModel.Text;
                _car.depot = ddlDepot.SelectedValue;
                _car.drivetrains = ddlDrivetrains.SelectedValue;
                _car.color = ddlColor.SelectedValue;
                _car.tonnage = decimal.Parse(tbTonnage.Text);
                _car.status = "A";
                _car.buy_in_date = DateTime.ParseExact(tbBuyInDate.Text, "yyyy-MM-dd", null);
                _car.buy_in_cost = decimal.Parse(tbBuyinCost.Text);
                _car.rent_price = decimal.Parse(tbRentPrice.Text);
                _car.last_rental_date = DateTime.MinValue;
                _car.last_rental_order_no = "";
                _car.license_no = tbLicense.Text;
                _car.number_of_rental = 0;
                _car.shopcode = ddlShop.SelectedValue;

                _dbAccess.InsertCar(_car);
                MessageBox.Show("Add car success", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }

        }

    }
}